<template>
  <div class="app-container">

    <el-table v-loading="loading" :data="letterList" @selection-change="handleSelectionChange"
              :row-class-name="tableRowClassName">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="ID" align="center" prop="lvId" />-->
      <el-table-column label="投诉人" align="center" prop="lvTsPl" />
<!--      <el-table-column label="申请流水号" align="center" prop="lvSl" />-->
      <el-table-column label="投诉人电话" align="center" prop="lvTsTel" />
      <el-table-column label="投诉渠道" align="center" prop="lvSource" >
        <template slot-scope="scope">
               <span v-if="scope.row.lvSource===1"> 来电  </span>
                 <span v-if="scope.row.lvSource===2"> 来访  </span>
                    <span v-if="scope.row.lvSource===3"> 互联网举报  </span>
        </template>
      </el-table-column>
      <el-table-column label="投诉单位" align="center" prop="lvTsEp" />
<!--      <el-table-column label="污染类型" align="center" prop="lvPtType" />-->
      <el-table-column label="投诉内容" align="center" prop="lvTsCt" />
<!--      <el-table-column label="信访时间" align="center" prop="lvStTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lvStTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="受理状态" align="center" prop="lvIsAccept">
        <template slot-scope="scope">
          <span v-if="scope.row.lvIsAccept===0" style="color: #004d8c"> 未受理  </span>
          <span v-if="scope.row.lvIsAccept===1" style="color: red"> 已受理  </span>
        </template>
      </el-table-column>
      <el-table-column label="办理状态" align="center" prop="lvDealState">
        <template slot-scope="scope">
          <span v-if="scope.row.lvDealState===0" > 审核中  </span>
          <span v-if="scope.row.lvDealState===1" > 调查整改中  </span>
          <span v-if="scope.row.lvDealState===2" > 已完成  </span>
        </template>
      </el-table-column>
      <el-table-column label="处理结果" align="center" prop="lvDealResult" />
      <el-table-column label="是否公示" align="center" prop="lvIsPub" >
        <template slot-scope="scope">
          <span v-if="scope.row.lvIsPub===0"> 公示  </span>
          <span v-if="scope.row.lvIsPub===1"> 不公示  </span>
        </template>
      </el-table-column>
<!--      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="照片" align="center" prop="lvPic" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="primary"
            type="primary"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['visit:letter:edit']"
          >指派</el-button>
<!--          <el-button-->
<!--            size="small"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['visit:letter:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改信访投诉对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label=" 执法大队人员列表">
          <!--              <el-checkbox v-model="menuExpand" @change="handleCheckedTreeExpand($event, 'menu')">展开/折叠</el-checkbox>-->
          <el-checkbox  v-model="checked" @change="checkedAll">全选/全不选</el-checkbox>
          <el-tree
            :data="data"
            show-checkbox
            default-expand-all
            :default-checked-keys="selectArr"
            node-key="id"
            ref="vuetree"
            class="tree-border"
            highlight-current
            @check-change="handleCheckChange"
            :props="defaultProps">
          </el-tree>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer tianjie_button">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLetter, getLetter, delLetter, addLetter, updateLetter, exportLetter } from "@/api/visit/letter";
import {listUser} from "@/api/system/user";
export default {
  name: "Letter",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      checked:false,
      userName:'',
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      selectArr:[],
      // 选中数组
      ids: [],
      data: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 信访投诉表格数据
      letterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lvTsPl: null,
        lvSl: null,
        lvTsTel: null,
        lvSource: null,
        lvTsEp: null,
        lvPtType: null,
        lvTsCt: null,
        lvStTime: null,
        lvIsAccept: null,
        lvDealState: null,
        lvDealResult: null,
        lvIsPub: null,
        lvPic: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getUser();
  },
  methods: {
    tableRowClassName({ rowIndex }) {
      if ((rowIndex + 1) % 2 === 0) {
        return 'oddRow';
      }
      return 'evenRow';
    },
    getUser(){
      listUser(this.queryParams).then(res=>{
          var list=res.rows;
          var arr=[];
          var index=0;
         list.map(function(item){
           if(!item.createBy.startsWith('admin')){
             var value={
               id: item.userId,
               label: item.createBy,
             }
             arr.push(value)
           }
         })
        this.data=arr;
      })
    },
    /** 查询信访投诉列表 */
    getList() {
      this.loading = true;
      listLetter(this.queryParams).then(response => {
        this.letterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        lvId: null,
        lvAcceptP:null,
        lvTsPl: null,
        lvSl: null,
        lvTsTel: null,
        lvSource: null,
        lvTsEp: null,
        lvPtType: null,
        lvTsCt: null,
        lvStTime: null,
        lvIsAccept: null,
        lvDealState: null,
        lvDealResult: null,
        lvIsPub: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        lvPic: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.lvId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加信访投诉";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      if(row.lvAcceptP){
        var arr=row.lvAcceptP.split(",");
        this.selectArr=arr;
      }else{
        this.selectArr=[];
      }
      this.reset();
      const lvId = row.lvId || this.ids
      getLetter(lvId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改信访投诉";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if(this.userName==""){
        this.msgSuccess("请选择用户");
        return;
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.lvId != null) {
            updateLetter(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLetter(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const lvIds = row.lvId || this.ids;
      this.$confirm('是否确认删除信访投诉编号为"' + lvIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delLetter(lvIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有信访投诉数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportLetter(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    checkedAll(){
      if (this.checked) {
        //全选
        this.$refs.vuetree.setCheckedNodes(this.data);
      }else{
        //取消选中
        this.$refs.vuetree.setCheckedKeys([]);
      }
    },
    handleCheckChange(data, checked){
      var arr= this.$refs.vuetree.getCheckedNodes();
      if(arr){
        var usrArr=[];
        for(var i=0;i<arr.length;i++){
          usrArr.push(arr[i].id);

        }
        var name=usrArr.join(",");
        this.userName=name;
        this.form.lvAcceptP=name;
        this.form.lvIsAccept = 1;
        this.form.lvDealState=1;
      }else{
        this.form.lvDealState=0;
      }
    }
  }
};
</script>
<style>
.oddRow {
  background-color: #00FF00;
}

.evenRow {
  background-color: rgba(22,78,97, 0.55);
}
</style>
